<template>
  <el-dialog
    title="密码修改"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <span slot="footer" class="dialog-footer">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="用户名" align="left">
          {{ ruleForm.userName }}
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassword" align="center">
          <el-input v-model="ruleForm.oldPassword" type="password" clearable />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" align="center">
          <el-input v-model="ruleForm.newPassword" type="password" clearable />
        </el-form-item>
      </el-form>
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateUserPassword } from '@/api/user'
export default {
  name: 'UserPasswordUpdate',
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        id: '',
        oldPassword: '',
        newPassword: '',
        userName: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '旧密码不能为空', trigger: 'change' }
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateUserPassword({
            id: this.ruleForm.id,
            oldPassword: this.ruleForm.oldPassword,
            newPassword: this.ruleForm.newPassword
          }).then(() => {
            this.$message.success('操作成功')
            this.$parent.init()
            this.handleClose()
          })
        } else {
          return false
        }
      })
    },
    init(row) {
      this.dialogVisible = true
      this.ruleForm.id = row.id
      this.ruleForm.userName = row.userName
    },
    handleClose() {
      this.dialogVisible = false
      this.ruleForm = {}
      this.$nextTick(() => {
        this.$parent.UserPasswordUpdateVisible = false
      })
    }
  }
}
</script>

<style scoped>

</style>
